package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.service.UserConfigService;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ExpenseEnum;
import com.xbongbong.saas.enums.business.ReimbursementFormEnum;
import com.xbongbong.saas.toolbox.util.UserConfigUtil;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.model.UserConfigModel;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/12/16 20:23
 */
@Component
public class BusinessListHelp {

    @Resource
    private UserConfigModel userConfigModel;
    @Resource
    private UserConfigService userConfigService;




    /**
     * 设置字段排序
     * @param subBusinessType
     * @param businessType
     * @param formId
     * @param appId
     * @param corpid
     * @param listGroupId
     * @param userId
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author 吴峰
     * @date 2021/12/16 20:48
     */
    public Map<String, String> getSortMap(Integer subBusinessType, Integer businessType, Long formId, Long appId, String corpid, Long listGroupId, String userId, Map<String, String> sortMap ){
        if (sortMap != null && !sortMap.isEmpty()) {
            return sortMap;
        }
        if (Objects.equals(subBusinessType, XbbRefTypeEnum.WAIT_PURCHASE.getCode()) ||
                Objects.equals(subBusinessType, XbbRefTypeEnum.WAIT_PRODUCE.getCode())) {
            return sortMap;
        }
        String key = UserConfigUtil.getKey(subBusinessType, appId, formId);
        String defaultSortConfigPrefix = userConfigService.getDefaultSortConfigPrefix(businessType, subBusinessType,listGroupId);
        String defaultSortConfigAlias = defaultSortConfigPrefix + (key.isEmpty() ? "" : "_" + key);
        UserConfigEntity userConfig = userConfigModel.getByUserId(userId, corpid,
                defaultSortConfigAlias);

        boolean isReimbursementForm = Objects.equals(businessType, XbbRefTypeEnum.REIMBURSEMENT_FORM.getCode());
        boolean isExpense = Objects.equals(businessType, XbbRefTypeEnum.EXPENSE.getCode());

        if (userConfig == null || StringUtil.isEmpty(userConfig.getConfigAlias())) {
            if (!isReimbursementForm && !isExpense) {
                return sortMap;
            }

            if (isReimbursementForm) {
                sortMap.put("sort", SortOrder.DESC.toString());
                sortMap.put("field", ReimbursementFormEnum.SUBMIT_TIME.getAttr());

            }
            if (isExpense) {
                sortMap.put("sort", SortOrder.DESC.toString());
                sortMap.put("field", ExpenseEnum.CONSUME_TIME.getAttr());
            }
            return sortMap;
        }
        JSONObject sort = JSONObject.parseObject(userConfig.getConfigValue());
        sortMap.put("sort", sort.getString("sortType"));
        sortMap.put("field", sort.getString("sortAttr"));
        return sortMap;
    }
}
